using UnityEngine;
using QFramework;

// 1.请在菜单 编辑器扩展/Namespace Settings 里设置命名空间
// 2.命名空间更改后，生成代码之后，需要把逻辑代码文件（非 Designer）的命名空间手动更改
namespace QFramework.Example
{
	public partial class UGUIInventoryExample : ViewController
	{
		private void Awake()
		{
			ItemKit.LoadItemDatabase("ExampleItemDatabase");
			
			ItemKit.CreateSlotGroup("背包")
				.CreateSlotsByCount(20);
			
			ItemKit.CreateSlotGroup("物品栏")
				.CreateSlot(ItemKit.ItemByKey[Items.item_iron_sword], 3)
				.CreateSlot(ItemKit.ItemByKey[Items.item_apple], 8)
				.CreateSlotsByCount(3);

			ItemKit.CreateSlotGroup("武器")
				.CreateSlot(null, 0)
				.Condition(item => item.GetBoolean("IsWeapon"));

			ItemKit.CreateSlotGroup("宝箱")
				.CreateSlotsByCount(6);

			var weaponSlot = ItemKit.GetSlotGroupByKey("武器").Slots[0];

			weaponSlot.Changed.Register(() =>
			{
				if (weaponSlot.Count > 0)
				{
					Debug.Log("切换武器：" + weaponSlot.Item.GetName);
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
		}
		
		void Start()
		{
			// 增加物品
			BtnAddItem1.onClick.AddListener(() =>
			{
				var result = ItemKit.GetSlotGroupByKey("物品栏").AddItem(Items.item_iron_sword);
				if (!result.Succeed)
				{
					if (result.MessageType == SlotGroup.MessageTypes.Full)
					{
						Debug.Log("物品栏已满！");
					}
				}
			});
			BtnAddItem2.onClick.AddListener(() =>
			{
				var result = ItemKit.GetSlotGroupByKey("物品栏").AddItem(Items.item_apple, 13);
				if (!result.Succeed)
				{
					if (result.MessageType == SlotGroup.MessageTypes.Full)
					{
						Debug.Log("物品栏已满！");
					}
				}
			});
			
			//减少物品
			BtnSubItem1.onClick.AddListener(() =>
			{
				ItemKit.GetSlotGroupByKey("物品栏").SubItem(Items.item_iron_sword);
			});
			BtnSubItem2.onClick.AddListener(() =>
			{
				ItemKit.GetSlotGroupByKey("物品栏").SubItem(Items.item_apple);
			});
		}
		
	}
}
